package com.bjpowerrnode.streamTest;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Optional;
import java.util.stream.Stream;

public class KReduce {
    public static void main(String[] args) {

        test4();
    }

    public static void test1(){
        Integer reduce = Stream.of(1, 2, 3, 4).reduce(100, (acc, item) -> {
                    System.out.println("acc: " + acc);
                    acc += item;
                    System.out.println("item: " + item);
                    System.out.println("acc+ :" + acc);
                    System.out.println("------------");
                    return acc;
                }
        );
        System.out.println(reduce);
    }
    public static void test2(){
        Integer reduce = Stream.of(1, 2, 3, 4).reduce((acc, item) -> {
                    System.out.println("acc: " + acc);
                    acc += item;
                    System.out.println("item: " + item);
                    System.out.println("acc+ :" + acc);
                    System.out.println("------------");
                    return acc;
                }
        ).get();
        System.out.println(reduce);
    }

    public static void test3(){
        ArrayList<Integer> newList = new ArrayList<>();

        ArrayList<Integer> accResult_ = Stream.of(2, 3, 4)
                .reduce(newList,
                        (acc, item) -> {
                            acc.add(item);
                            System.out.println("item: " + item);
                            System.out.println("acc+ : " + acc);
                            System.out.println("BiFunction");
                            return acc;
                        }, (acc, item) -> null);
        System.out.println("accResult_: " + accResult_);
    }

    public static void test4(){
        Optional<String> reduce = Arrays.asList("sun", "xue", "teng").stream()
                .reduce((result, item) -> result + "&&" + item);
        reduce.stream().forEach(System.out::println);
        System.out.println(reduce.get());
    }
}
